from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
from pandas import DataFrame

from api.api_db import delete_drcs, update_drcs
from api.api_view import ThCreateReview
from api.api_project import Public_Data, create_drcs_dict

from view.table_col_list import drcs_col_model_list
from view.tab_widget import Review_Table


class View_DRCS(QWidget):
    def __init__(self):
        super().__init__()
        self.view_name = "tab_drcs"
        self.data = Public_Data()
        self.setupUi()
        self.bind()
        self.th_drcs = ThCreateReview([])
        # self.init_cmb()

    def setupUi(self):
        # region 布局
        # 整体布局为垂直布局
        lt_pg = QVBoxLayout(self)
        fm_ctrl = QFrame()

        lt_pg.addWidget(fm_ctrl)
        lt_header = QHBoxLayout(fm_ctrl)

        # endregion

        # region 项目、制造厂及采购包选择

        # 左侧项目cmb
        left_spacer = QSpacerItem(120, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)

        #     lt_header.addWidget(item)
        lt_header.addItem(left_spacer)

        # endregion

        # region 操作DRCS和IDRS

        self.btn_delete = QPushButton("删除")
        self.btn_reprush = QPushButton("刷新信息")
        self.btn_create = QPushButton("发起流程")
        self.btn_save = QPushButton("保存修改")
        lt_header.addWidget(self.btn_save)
        lt_header.addWidget(self.btn_create)
        lt_header.addWidget(self.btn_reprush)
        lt_header.addWidget(self.btn_delete)
        # endregion

        # region 信息展示标签和表格
        self.lb_message = QLabel(text="已处理了XX个DRCS，待处理的DRCS数量为")
        self.lb_message.setAutoFillBackground(False)
        self.lb_message.setWordWrap(True)
        lt_pg.addWidget(self.lb_message, 0, Qt.AlignLeft)

        self.table = Review_Table("drcs_doc", drcs_col_model_list)
        lt_pg.addWidget(self.table)

        # endregion

    def bind(self):

        self.btn_create.clicked.connect(self.start_drcs_flow)
        self.btn_reprush.clicked.connect(self.table.reset_data)
        self.btn_save.clicked.connect(lambda: self.save_drcs())
        self.btn_delete.clicked.connect(self.delete_selected)

    def start_drcs_flow(self):
        if self.table.change_dic:
            self.save_drcs()

        drcs_dict_list = create_drcs_dict(self.table.datas)
        if drcs_dict_list:
            self.th_drcs.datas = drcs_dict_list
            self.th_drcs.signal_send.connect(self.create_drcs_result)
            self.th_drcs.start()
        else:
            QMessageBox.information(self, "创建DRCS流程信息", "没有需创建DRCS流程的文件")

    def save_drcs(self):
        update_drcs(self.table.change_dic)

        self.table.change_dic = {}
        self.table.reset_data()

    def create_drcs_result(self, result=0):
        msg = "创建不成功"
        if result:
            msg = "创建完成"
        QMessageBox.information(self, "创建DRCS结果", msg)

    def delete_selected(self):
        result = QMessageBox.question(self, "删除DRCS", "你确定要删除所选的DRCS么？")
        if result == QMessageBox.StandardButton.Yes:
            df_selected: DataFrame = self.table.get_selected_rows()
            drcs_id_list = df_selected["drcs_id"].tolist()
            for drcs_id in drcs_id_list:
                delete_drcs(drcs_id)
            self.table.reset_data()
